package org.pz.handler;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.pz.entity.MallResult;
import org.pz.http.HttpUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录成功处理器
 * 1. 保存 session，或者生成 token 两种方式任选其一
 * 2. 返回前端信息
 * */
@Component
@Slf4j
public class LoginSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //认证流程第三步：验证成功，响应前端
        System.out.println(JSONObject.toJSONString( SecurityContextHolder.getContext().getAuthentication()) );

        //响应给前端
        HttpUtils.parseResponseBody(JSONObject.toJSONString(MallResult.success()), response);
    }
}
